import java.util.*;
import java.io.*;

public class Example {
    public static void main(String args[]) {
        File root = new File("Texts");
        processFolder(root);
    }

    private static void processFolder(File folder) {
        File[] files = folder.listFiles();
        for (File file : files) {
            if (file.isDirectory()) processFolder(file);
            else {
                StringBuilder outputFilePath = new StringBuilder(file.getPath());
                outputFilePath.insert(outputFilePath.length() - 4, " - Modified");
                File outputFile = new File(outputFilePath.toString());
                try (Scanner in = new Scanner(file);
                     PrintWriter out = new PrintWriter(outputFile);) {
                    while (in.hasNext()) {
                        StringBuilder line = new StringBuilder(in.nextLine());
                        String replaced = "Exercise0_0";
                        int current = 0;
                        while (true) {
                            current = findWord(line);
                            if (current == -1) break;
                            String modifiedWord = modifiedWord(line.substring(current, current + replaced.length()));
                            line.replace(current, current + replaced.length(), modifiedWord);
                        }
                        out.println(line);
                    }
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    //该函数用于找到字符串中第一个符合"Exercisei_j"格式的字串的下标
    //当不存在这样的子串时，返回-1
    private static int findWord(StringBuilder str) {
        int current = 0;
        String exercise = "Exercise";
        while (current <= str.length() - 1) {
            current = str.indexOf(exercise, current);
            //已经无法找到"Exercise"
            if (current == -1) {
                break;
            }
            //找到"Exercise"，检查后续文本是否符合要求
            int underlineIndex = current + exercise.length() + 1;
            //不符合要求，继续向下寻找
            if (underlineIndex >= str.length() || str.charAt(underlineIndex) != '_') {
                current += exercise.length();
                continue;
            }
            //符合要求
            if (isDigit(str.charAt(underlineIndex - 1)) && isDigit(str.charAt(underlineIndex + 1))
                    && !isDigit(str.charAt(underlineIndex + 2))) return current;
            else {
                current += exercise.length();
            }
        }
        return -1;
    }

    private static boolean isDigit(char ch) {
        return ch >= '0' && ch <= '9';
    }

    private static String modifiedWord(String srcWord) {
        return "Exercise0" + srcWord.substring(8, 10) + "0" + srcWord.substring(10, 11);
    }
}
